"""
审计日志 | 接口
"""

from fastapi import (
    APIRouter,
    Depends,
)

import utils.enum_utils as enum
from biz import audit_log_biz
from components.audit_log import audit
from components.depends import admin_required
from components.global_param import get_user_id
from components.logger import log
from components.response import OKWithDataResponse
from models.request import (
    AuditLogDetailRequest,
    AuditLogListRequest,
    AuditLogPathField,
)
from models.response import (
    AuditLogDetailResponse,
    AuditLogListResponse,
)
from models.vo import (
    AuditLogDetailVO,
    AuditLogListPage,
)
from utils.data_utils.special import ObjectIdStr

audit_log_router: APIRouter = APIRouter(prefix="/audit_log")
""" 审计日志 路由 """


@audit_log_router.get(
    path="/{audit_log_id}/",
    summary="审计日志 | 详情",
    response_model=AuditLogDetailResponse,
    dependencies=[Depends(admin_required)],
)
@audit(
    model=enum.Model.AUDIT_LOG,
    operation=enum.ModelOperation.DETAIL,
    po_id="audit_log_id",
)
async def audit_log_detail(audit_log_id: ObjectIdStr = AuditLogPathField.audit_log_id) -> OKWithDataResponse:
    request = AuditLogDetailRequest(audit_log_id=audit_log_id)
    vo: AuditLogDetailVO = audit_log_biz.AuditLogGetter.detail(request)
    log.info(f"[审计日志][详情] user_id: {get_user_id()} audit_log_id: {audit_log_id}")

    return OKWithDataResponse(vo)


@audit_log_router.get(
    path="/",
    summary="审计日志 | 列表",
    response_model=AuditLogListResponse,
    dependencies=[Depends(admin_required)],
)
@audit(
    model=enum.Model.AUDIT_LOG,
    operation=enum.ModelOperation.LIST,
    po_id="response.data.data_list.audit_log_id",
)
async def audit_log_list(request: AuditLogListRequest = Depends(AuditLogListRequest.from_query)) -> OKWithDataResponse:
    vo: AuditLogListPage = audit_log_biz.AuditLogGetter.list(request)
    log.info(f"[审计日志][列表] user_id: {get_user_id()}")

    return OKWithDataResponse(vo)
